import Vue from 'vue'
import Router from 'vue-router'
import constantRoutes from './routers'
import { loadMenu } from '@/utils/loadMenu.js'
import store from '@/store'
Vue.use(Router)
const createRouter = () => new Router({
  // mode: 'history', // require service support
  
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes
})
const router = createRouter()
router.beforeEach((to,from,next)=>{
  //如果是3级代理就不显示下级代理列表
  if(store.state.user.info.level == 3 || store.state.user.info.level == '3'){
    constantRoutes.forEach((item)=>{
        if(item.path == '/offline'){
          item.children[1].meta.hide = true
        }
      
  })
  }else{
    constantRoutes.forEach((item)=>{
        if(item.path == '/offline'){
          item.children[1].meta.hide = false
        }
  })
  }

 setTimeout(()=>{
  next()
 },800)
})

router.afterEach((to, from) =>{
  // to: 跳转的目标路由对象
  // from: 跳转前的路由对象
  console.error(to,from)
})
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export function setRouterData (level) {
  console.log("setRouterData level:" + level);
}

export function setRouterMenus (menus) {
  
  // let menuslist = loadMenu(store.getters.menus);
  // console.log("store.getters.menus",store.state.user.menus)
  // for (var i = 0; i < menuslist.length; i++) {
  //   constantRoutes.push(menuslist[i])
  // }
  
  constantRoutes.push({ path: '*', redirect: '/404', hidden: true }); //404页面最后推入



  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router

}
export default router
